package com.scalar.chiptrack.operations.shippingRequest.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;

import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.operations.shippingRequest.dao.ShippingRequestDAO;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.ArrayList;


public class ShippingRequestSearchCommand extends ActionCommand
{
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {

        ShippingRequestForm shippingRequestForm     = ( ShippingRequestForm ) form;
        ShippingRequest shippingRequest             = shippingRequestForm.getShippingRequest();
        ActionForward actionForward                 = mapping.findForward( "search" );
        HashMap errors                              = new HashMap();
        HashMap info                                = new HashMap();

        try
        {

            ArrayList searchResultList = ShippingRequestDAO.getAllShipRequestDetails( null, shippingRequest );

            if( searchResultList != null && searchResultList.size() > 0 )
            {
                request.setAttribute( "searchList", searchResultList );
            }
            else
            {
                info.put( "Shipping Request matching the given search criteria could not be found", null );
            }

            request.setAttribute( "vendorList", ShippingRequestDAO.loadClassStroeLocations( null )  );
            request.setAttribute( "partNumberList", ShippingRequestDAO.getProductList( Constants.LOCATION_TYPE_CLASS_STORE ) );
            request.setAttribute( "yearList", ShippingRequestDAO.getYearList() );
            request.setAttribute( "statusList", ShippingRequestDAO.getStatusList() );
            shippingRequestForm.setShippingRequest( shippingRequest );

            actionForward = mapping.findForward("search");
            request.setAttribute( INFO_MESSAGES, info );
            request.setAttribute( ERROR_MESSAGES, errors );
        }
        catch( Exception  e )
        {
            throw new ScalarException("Exception while getting all the shipping request details" + e);
        }

        return actionForward;
    }
}